package com.aliyun.heiyu.powermaster.server.service;

import com.aliyun.heiyu.common.util.DateUtil;
import com.aliyun.heiyu.powermaster.server.dao.DeviceCommonDao;
import com.aliyun.heiyu.powermaster.server.dao.DictionaryConfigDao;
import com.aliyun.heiyu.powermaster.server.dao.StateGridCollectInfoDao;
import com.aliyun.heiyu.powermaster.server.dao.entity.DeviceIPDeviceIdDTO;
import com.aliyun.heiyu.powermaster.server.dao.impl.DeviceCommonImpl;
import com.aliyun.heiyu.powermaster.server.dao.impl.DictionaryConfigDaoImpl;
import com.aliyun.heiyu.powermaster.server.dao.impl.StateGridCollectInfoImpl;
import org.apache.commons.collections.CollectionUtils;

import java.util.*;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class StateGridPowerctlAgentServerPerformanceServiceImplRun {
    private static DeviceCommonDao deviceCommonDao = new DeviceCommonImpl();
    private static StateGridCollectInfoDao stateGridCollectInfoDao = new StateGridCollectInfoImpl();
    private static DictionaryConfigDao dictionaryConfigDao = new DictionaryConfigDaoImpl();

    private static final ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(300, 400, 5L,
        TimeUnit.MICROSECONDS, new ArrayBlockingQueue<Runnable>(1000));

    public void threadPoolTask() {
        List<DeviceIPDeviceIdDTO> list = deviceCommonDao.selectServerIp();
        if (list == null || list.size() <= 0) {
            return;
        }
        String deviceSwitch = dictionaryConfigDao.getValue("deviceSwitch");
        if (null != list || list.size() > 0) {
            for (DeviceIPDeviceIdDTO deviceIPDeviceId : list) {
                threadPoolExecutor.execute(new StateGridPowerctlAgentServerPerformanceServiceImpl(deviceSwitch, deviceIPDeviceId));
            }
        }
        list.clear();
    }

    /**
     * 用来测试 并发 两秒钟采集完成189个设备的动态数据
     */
    public void threadPoolTaskTest() {
        List<DeviceIPDeviceIdDTO> list = new ArrayList<>();
        Map<String, String> map = new HashMap<>();
        map.put("11.158.168.65", "1028");
        map.put("11.158.174.4", "1029");
        map.put("11.158.168.194", "1030");
        map.put("11.158.168.199", "1031");
        map.put("11.158.168.130", "1032");
        map.put("11.158.168.134", "1033");
        map.put("11.158.191.5", "1034");
        map.put("11.158.191.10", "1035");
        map.put("11.158.174.196", "1036");
        map.put("11.158.174.198", "1037");
        map.put("11.158.174.199", "1038");
        map.put("11.158.174.201", "1039");
        map.put("11.158.174.202", "1040");
        map.put("11.158.174.136", "1041");
        map.put("11.158.174.137", "1042");
        map.put("11.158.191.201", "1043");
        map.put("11.158.191.203", "1044");
        map.put("11.158.191.136", "1045");
        map.put("11.158.192.129", "1046");
        map.put("11.159.68.4", "1047");
        map.put("11.159.68.10", "1048");
        map.put("11.159.68.11", "1049");
        map.put("11.158.201.130", "1050");
        map.put("11.158.201.132", "1051");
        map.put("11.158.201.134", "1052");
        map.put("11.158.201.136", "1053");
        map.put("11.158.201.137", "1054");
        map.put("11.158.201.138", "1055");
        map.put("11.159.69.65", "1056");
        map.put("11.159.69.71", "1057");
        map.put("11.159.69.72", "1058");
        map.put("11.159.69.73", "1059");
        map.put("11.159.69.74", "1060");
        map.put("11.159.69.10", "1061");
        map.put("11.158.160.65", "1062");
        map.put("11.158.160.66", "1063");
        map.put("11.158.160.67", "1064");
        map.put("11.158.160.70", "1065");
        map.put("11.158.160.71", "1066");
        map.put("11.158.160.72", "1067");
        map.put("11.158.160.73", "1068");
        map.put("11.158.160.2", "1069");
        map.put("11.158.160.3", "1070");
        map.put("11.158.160.4", "1071");
        map.put("11.158.160.5", "1072");
        map.put("11.158.160.6", "1073");
        map.put("11.158.160.7", "1074");
        map.put("11.167.170.193", "1075");
        map.put("11.167.170.197", "1076");
        map.put("11.167.170.198", "1077");
        map.put("11.167.170.199", "1078");
        map.put("11.167.170.201", "1079");
        map.put("11.167.132.65", "1080");
        map.put("11.167.132.66", "1081");
        map.put("11.167.132.67", "1082");
        map.put("11.167.132.68", "1083");
        map.put("11.167.132.69", "1084");
        map.put("11.158.168.2", "1085");
        map.put("11.158.168.8", "1086");
        map.put("11.158.160.193", "1087");
        map.put("11.158.160.194", "1088");
        map.put("11.158.160.195", "1089");
        map.put("11.158.160.203", "1090");
        map.put("11.158.160.131", "1091");
        map.put("11.158.160.132", "1092");
        map.put("11.158.160.133", "1093");
        map.put("11.158.160.139", "1094");
        map.put("11.159.69.193", "1095");
        map.put("11.159.69.194", "1096");
        map.put("11.159.69.195", "1097");
        map.put("11.159.69.196", "1098");
        map.put("11.159.69.197", "1099");
        map.put("11.159.69.198", "1100");
        map.put("11.159.69.199", "1101");
        map.put("11.159.69.200", "1102");
        map.put("11.159.69.201", "1103");
        map.put("11.159.69.203", "1104");
        map.put("11.159.69.129", "1105");
        map.put("11.159.69.133", "1106");
        map.put("11.159.69.137", "1107");
        map.put("11.159.69.138", "1108");
        map.put("11.159.69.139", "1109");
        map.put("11.159.98.195", "1110");
        map.put("11.159.98.196", "1111");
        map.put("11.159.98.197", "1112");
        map.put("11.159.98.198", "1113");
        map.put("11.159.98.199", "1114");
        map.put("11.159.98.200", "1115");
        map.put("11.159.98.201", "1116");
        map.put("11.159.98.129", "1117");
        map.put("11.159.98.130", "1118");
        map.put("11.159.98.131", "1119");
        map.put("11.159.98.132", "1120");
        map.put("11.159.98.133", "1121");
        map.put("11.159.98.134", "1122");
        map.put("11.159.98.135", "1123");
        map.put("11.159.98.136", "1124");
        map.put("11.159.98.137", "1125");
        map.put("11.159.98.138", "1126");
        map.put("11.159.98.139", "1127");
        map.put("11.159.98.65", "1128");
        map.put("11.159.98.66", "1129");
        map.put("11.159.98.68", "1130");
        map.put("11.159.98.69", "1131");
        map.put("11.159.98.70", "1132");
        map.put("11.159.98.71", "1133");
        map.put("11.159.98.72", "1134");
        map.put("11.159.98.73", "1135");
        map.put("11.159.98.74", "1136");
        map.put("11.159.98.75", "1137");
        map.put("11.159.98.1", "1138");
        map.put("11.159.98.3", "1139");
        map.put("11.159.98.2", "1140");
        map.put("11.159.98.4", "1141");
        map.put("11.159.98.5", "1142");
        map.put("11.159.98.6", "1143");
        map.put("11.159.98.11", "1144");
        map.put("11.159.133.193", "1145");
        map.put("11.159.133.195", "1146");
        map.put("11.159.133.196", "1147");
        map.put("11.159.133.198", "1148");
        map.put("11.159.133.200", "1149");
        map.put("11.159.133.202", "1150");
        map.put("11.159.133.129", "1151");
        map.put("11.159.133.130", "1152");
        map.put("11.159.133.131", "1153");
        map.put("11.159.133.132", "1154");
        map.put("11.159.133.133", "1155");
        map.put("11.159.133.66", "1156");
        map.put("11.159.133.67", "1157");
        map.put("11.159.133.68", "1158");
        map.put("11.159.133.69", "1159");
        map.put("11.159.133.71", "1160");
        map.put("11.159.133.72", "1161");
        map.put("11.159.133.73", "1162");
        map.put("11.159.133.1", "1163");
        map.put("11.159.133.2", "1164");
        map.put("11.159.133.3", "1165");
        map.put("11.159.133.4", "1166");
        map.put("11.159.133.5", "1167");
        map.put("11.159.133.6", "1168");
        map.put("11.159.156.194", "1169");
        map.put("11.159.156.195", "1170");
        map.put("11.159.156.197", "1171");
        map.put("11.159.156.198", "1172");
        map.put("11.159.156.201", "1173");
        map.put("11.159.156.131", "1174");
        map.put("11.159.156.136", "1175");
        map.put("11.159.156.65", "1176");
        map.put("11.159.156.68", "1177");
        map.put("11.159.156.72", "1178");
        map.put("11.159.156.73", "1179");
        map.put("11.159.156.1", "1180");
        map.put("11.159.156.4", "1181");
        map.put("11.159.160.195", "1182");
        map.put("11.159.160.196", "1183");
        map.put("11.159.160.200", "1184");
        map.put("11.159.160.201", "1185");
        map.put("11.159.160.72", "1186");
        map.put("11.159.160.73", "1187");
        map.put("11.159.160.129", "1188");
        map.put("11.159.160.131", "1189");
        map.put("11.159.160.133", "1190");
        map.put("11.159.160.136", "1191");
        map.put("11.159.160.2", "1192");
        map.put("11.159.160.3", "1193");
        map.put("11.159.160.10", "1194");
        map.put("11.159.195.197", "1195");
        map.put("11.159.195.199", "1196");
        map.put("11.159.195.200", "1197");
        map.put("11.159.195.201", "1198");
        map.put("11.159.195.202", "1199");
        map.put("11.159.195.134", "1200");
        map.put("11.159.195.136", "1201");
        map.put("11.159.195.66", "1202");
        map.put("11.159.195.70", "1203");
        map.put("11.159.195.71", "1204");
        map.put("11.159.195.72", "1205");
        map.put("11.159.195.73", "1206");
        map.put("11.159.195.74", "1207");
        map.put("11.159.195.2", "1208");
        map.put("11.159.195.4", "1209");
        map.put("11.159.195.6", "1210");
        map.put("11.159.195.9", "1211");
        map.put("11.159.195.10", "1212");
        map.put("11.159.195.11", "1213");
        map.put("11.163.93.132", "1214");
        map.put("11.163.93.6", "1215");
        map.put("11.166.16.201", "1216");
        for (Map.Entry<String, String> m : map.entrySet()) {
            System.out.println("key:" + m.getKey() + " value:" + m.getValue());
            DeviceIPDeviceIdDTO dto = new DeviceIPDeviceIdDTO();
            dto.setIp(m.getKey());
            dto.setDevice_id(m.getValue());
            dto.setServer_type("x86");
            list.add(dto);
        }
        if (CollectionUtils.isEmpty(list)) {
            return;
        }
        String deviceSwitch = dictionaryConfigDao.getValue("deviceSwitch");

        System.out.println("开始时间" + new DateUtil().getStringDate(new Date()));
        if (!CollectionUtils.isEmpty(list)) {
            for (DeviceIPDeviceIdDTO deviceIPDeviceId : list) {
                threadPoolExecutor.execute(new StateGridPowerctlAgentServerPerformanceServiceImpl(deviceSwitch, deviceIPDeviceId));
            }
        }
        list.clear();
        map.clear();
        System.out.println("结束时间" + new DateUtil().getStringDate(new Date()));

    }

    public static void main(String[] args) {
        StateGridPowerctlAgentServerPerformanceServiceImplRun ss = new StateGridPowerctlAgentServerPerformanceServiceImplRun();
        try {
            ss.threadPoolTaskTest();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
